﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracy.DataModel;
using SharedLib;
namespace TracyGUI
{
    public partial class FormSelection<T> : Form
    {
        public event EventHandler<GenericEventArgs<List<T>>> SelectionConfirmed;
        private bool _dialogMode;
        private FormSelection()
        {
            InitializeComponent();
            this.Icon = SharedResource.Tracy;
        }
        public FormSelection(string title, string message, bool dialogMode):this()
        {
            this._dialogMode = dialogMode;
            this.Text = title;
            labelText.Text = message;
        }
        public void AddItem(T item)
        {
            if (!this.Visible && !_dialogMode) Show();
            checkedListBoxMain.Items.Add(item, true);
        }

        public void ClearItems()
        {
            checkedListBoxMain.Items.Clear();
        }

        public List<T> GetSelectedItems()
        {
            List<T> rtn = new List<T>();
            foreach (T item in checkedListBoxMain.CheckedItems)
            {
                rtn.Add(item);
            }
            return rtn;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            checkedListBoxMain.Items.Clear();
            if (_dialogMode)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                Close();
            }
            else
            {
                Hide();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (_dialogMode)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
            {

                List<T> rtn = GetSelectedItems();
                if (rtn.Count > 0)
                {
                    if (SelectionConfirmed != null)
                    {
                        SelectionConfirmed.InvokeEvent(this, new GenericEventArgs<List<T>>(rtn));
                    }
                }
                checkedListBoxMain.Items.Clear();
                Hide();
            }
        }

        private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
        {
            if (!_isUpdatingUI)
            {
                for (int i = 0; i < checkedListBoxMain.Items.Count; i++)
                {
                    checkedListBoxMain.SetItemChecked(i, checkBoxAll.Checked);
                }
            }
            
        }

        bool _isUpdatingUI = false;
        private void checkedListBoxMain_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            
            if (e.NewValue == CheckState.Unchecked)
            {
                _isUpdatingUI = true;
                checkBoxAll.Checked = false;
                _isUpdatingUI = false;
            }
            
        }
    }
}
